// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VectorNavOrientation.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Hardware.VectorNavOrientation
{
    using System;
    using VectorNav.Devices;

    /// <summary>
    /// Yaw/Pitch/Roll container struct
    /// </summary>
    public struct AttitudeSensorReading
    {
        /// <summary>
        /// Gets or sets Yaw angle in radians
        /// </summary>
        public double YawInRads { get; set; }

        /// <summary>
        /// Gets or sets Pitch angle in radians
        /// </summary>
        public double PitchInRads { get; set; }

        /// <summary>
        /// Gets or sets Roll angle in radians
        /// </summary>
        public double RollInRads { get; set; }

        /// <summary>
        /// Gets or sets reading timestamp. Set by the external sensor API.
        /// </summary>
        public DateTime TimeStamp { get; set; }
    }

    /// <summary>
    /// Wrapper around VectorNav chip
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
        "Microsoft.Design",
        "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
        Justification = "VnDevice gets cleaned up in OnDeactivated()")]
    public class VectorNavAttitudeSensor
    {
        /// <summary>
        /// Generic VN device
        /// </summary>
        private VnDevice device;

        /// <summary>
        /// Connects to VectorNav VN100 device. Will throw if there are problems with connecting.
        /// </summary>
        /// <param name="comPortName">COM port on which VN100 device sits I.e. "COM9"</param>
        /// <param name="baudRate">Baud rate, i.e. 115200</param>
        public void ConnectVN100(string comPortName, int baudRate = 115200)
        {
            this.device = new Vn100(comPortName, baudRate);

            this.device.Connect();            
        }

        /// <summary>
        /// Disconnect VectorNav device. 
        /// </summary>
        public void Disconnect()
        {
            if (null != this.device && this.device.IsConnected)
            {
                this.device.Disconnect();

                this.device.Dispose();
            }
        }

        /// <summary>
        /// Get current sensor orientation readings
        /// </summary>
        /// <param name="ypr">Yaw/Pitch/Roll out parameter</param>
        /// <returns>True if new data is read, false otherwise</returns>
        public bool Read(ref AttitudeSensorReading ypr)
        {
            CompositeData compositeDate = this.device.CurrentMeasurements;

            if (null != compositeDate)
            {
                ypr.YawInRads = compositeDate.YawPitchRoll.Yaw;
                ypr.PitchInRads = compositeDate.YawPitchRoll.Pitch;
                ypr.RollInRads = compositeDate.YawPitchRoll.Roll;
                ypr.TimeStamp = compositeDate.TimeStamp;

                return true;
            }

            return false;
        }
    }
}
